package com.example.springbootapplication.listener;

import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * 自定义spring 事件
 * @Author acy
 * @Date 2019/04/27
 */
public class BeforeConfigFileApplicationListener implements Ordered,SmartApplicationListener {


	@Override
	public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
		return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType)
				|| ApplicationPreparedEvent.class.isAssignableFrom(eventType);
	}

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof ApplicationEnvironmentPreparedEvent) {
			ApplicationEnvironmentPreparedEvent environmentPreparedEvent = (ApplicationEnvironmentPreparedEvent) event;
			ConfigurableEnvironment environment = environmentPreparedEvent.getEnvironment();
			System.out.println("environment.getProperty(\"name\")"+environment.getProperty("name"));
		}
		if (event instanceof ApplicationPreparedEvent) {
		}
	}
	//比ConfigFileApplicationListener优先级高
	@Override
	public int getOrder() {
		return ConfigFileApplicationListener.DEFAULT_ORDER-1;
	}
}
